package vip.mall.modular.login.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import vip.mall.common.enums.CommonResponseCode;
import vip.mall.common.exception.CommonException;
import vip.mall.modular.login.params.LoginParam;
import vip.mall.modular.login.service.LoginService;
import vip.mall.modular.user.entity.User;
import vip.mall.modular.user.service.UserService;

/**
 * @author ASUS
 * @Date 2025/2/13 14:14
 * @Descripition
 */
@Service
public class LoginServiceImpl implements LoginService {
    @Resource
    private UserService userService;
    @Override
    public boolean customerLogin(LoginParam loginParam) throws CommonException {
        LambdaQueryWrapper<User> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(User::getRoleFlag,0);
        lambdaQueryWrapper.eq(User::getUsername,loginParam.getUsername());
        User user = userService.getOne(lambdaQueryWrapper);
        if (ObjectUtil.isNotNull(user))
            return user.getPassword().equals(loginParam.getPassword());
        else throw new CommonException(CommonResponseCode.FAIL,"用户不存在");
    }

    @Override
    public boolean adminLogin(LoginParam loginParam) throws CommonException {
        LambdaQueryWrapper<User> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(User::getRoleFlag,1);
        lambdaQueryWrapper.eq(User::getUsername,loginParam.getUsername());
        User user = userService.getOne(lambdaQueryWrapper);
        if (ObjectUtil.isNotNull(user))
            return user.getPassword().equals(loginParam.getPassword());
        else throw new CommonException(CommonResponseCode.FAIL,"该账户非管理员账户");
    }
}
